unit CHDesign;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, ToolWin, ComCtrls, ImgList, ExtCtrls, ActnList, StdCtrls, AYLabel,rmConst,
  Grids, AYClass_TLB, AYUtils, AYConst,DB,
  LDSPanel, LDSLabel,LDSEdit,LDSCheckBox,
  LDSChild,CHDesigner;

type
  TFmCHDesign = class(TForm)
    MainMenuCH: TMainMenu;
    MenuFile: TMenuItem;
    ToolBarCH: TToolBar;
    ImageListBars: TImageList;
    ActionListCH: TActionList;
    ActionCHLabel: TAction;
    ActionCHEdit: TAction;
    ActionCHMemo: TAction;
    ActionCHDate: TAction;
    ActionCHCheck: TAction;
    ActionCHGrid: TAction;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ActionNone: TAction;
    MenuSave: TMenuItem;
    MenuOpen: TMenuItem;
    ActionCHImage: TAction;
    ToolButton8: TToolButton;
    ActionCHShape: TAction;
    ToolButton9: TToolButton;
    MenuSaveAs: TMenuItem;
    MenuDivSave: TMenuItem;
    MenuDivSaveAs: TMenuItem;
    MenuExit: TMenuItem;
    ActionCHBevel: TAction;
    ToolButton10: TToolButton;
    MenuTool: TMenuItem;
    ActionCHSeal: TAction;
    ToolButton11: TToolButton;
    CHDesigner: TCHDesigner;
    ToolButton12: TToolButton;
    ActionCHInspector: TAction;
    ToolButton13: TToolButton;
    ActionCHDesign: TAction;
    ToolButtonDesign: TToolButton;
    ToolButton15: TToolButton;
    ActionCHPanel: TAction;
    ToolButton14: TToolButton;
    PanelCHChild: TPanel;
    ScrollBoxCHChild: TScrollBox;
    StatusBarInfo: TStatusBar;
    MenuDivPrint: TMenuItem;
    MenuPrint: TMenuItem;
    MenuPreview: TMenuItem;
    procedure ActionCHLabelExecute(Sender: TObject);
    procedure MenuSaveClick(Sender: TObject);
    procedure MenuOpenClick(Sender: TObject);
    procedure MenuSaveAsClick(Sender: TObject);
    procedure MenuExitClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure ActionCHInspectorExecute(Sender: TObject);
    procedure CHDesignerGetComponentsControl(Sender: TObject; var AControl: TWinControl);
    procedure CHDesignerCreateCHControl(Sender: TObject; X, Y: Integer; var ASuccess: Boolean);
    procedure ActionCHDesignExecute(Sender: TObject);
    procedure CHDesignerDesignCHControl(Sender: TObject; ADesign: Boolean);
    procedure CHDesignerSelectCHControl(Sender: TObject; ASelection: TCHControlList);
    procedure MenuPrintClick(Sender: TObject);
    procedure MenuPreviewClick(Sender: TObject);
  private
    FObjType: Byte;
    FCHChild: TFormLDSChild;
    FSaveDialog: TSaveDialog;
    FOpenDialog: TOpenDialog;
    FOpenRepDialog: TOpenDialog;
    FRepFileName: String;
    procedure SetNoPause;
    procedure CreateCHControl(AParent: TWinControl; X, Y: Integer);
    function GetOpenRepDialog: TOpenDialog;
    procedure OpenRepFile;
    procedure DoPreviewAfterPrint(Sender: TObject; var ASuccess: Boolean);
    procedure DoPreviewBeforePrint(Sender: TObject; var ASuccess: Boolean);
    procedure DoGetPreviewRepList(Sender: TObject; var AIStrings: IAYStrings; var ADefaultIndex: Integer);
    procedure DoGetPrintRep(Sender: TObject; var ARepData: OleVariant; APNBM: String; APreview: Boolean);
  protected
    property CHChild: TFormLDSChild read FCHChild;
    property ObjType: Byte read FObjType write FObjType;
    property OpenRepDialog: TOpenDialog read GetOpenRepDialog;
    property RepFileName: String read FRepFileName write FRepFileName;
  public
    procedure LoadFromStream(Stream: TStream);
    procedure SaveToStream(Stream: TStream);
  end;

implementation

{$R *.DFM}

type
  TControlClass = class of TControl;

Const
  CHResVer = 'CHResVer3';
  CHClass: Array[0..7] Of TControlClass =
           (Nil, TLDSLabel,TLDSEdit,nil,nil,nil,nil,TLDSPanel);

{ TFmCHDesign }

procedure TFmCHDesign.CreateCHControl(AParent: TWinControl; X, Y: Integer);
var
  oControl: TControl;
begin
  if (ObjType > 0) and (ToolBarCH.Buttons[ObjType].Down) then
  begin
    oControl := CHClass[ObjType].Create(CHChild);
    With oControl Do begin
      Parent := AParent;
      Name := GetCHComponentName(ClassName);
      Left := X;
      Top  := Y;
      ShowHint := True;
      Visible := True;
      BringToFront;
      ActionNone.Execute;
    end;
    With CHDesigner Do
    begin
      DelSelection(Nil, True);
      AddSelection(oControl);
      RefreshObjectList;
      ControlProperty(False);
    end;
  end;
end;

procedure TFmCHDesign.ActionCHLabelExecute(Sender: TObject);
begin
  With ToolBarCH Do begin
    Buttons[ObjType].Down := False;
    ObjType := (Sender As TAction).Tag;
    Buttons[ObjType].Down := Not Buttons[ObjType].Down;
  end;
end;

procedure TFmCHDesign.LoadFromStream(Stream: TStream);
var
  i: Integer;
begin
  CHDesigner.InitDesigner;
  With CHChild Do
  begin
    Visible := False;
    ClearCHReport;
    LoadFromStream(Stream);
    ClearNameNumList;
    For i := 0 To ControlCount - 1 Do
    begin
      InitNameNumList(Controls[i].ClassName, Controls[i].Name);
    {  if Controls[i] is TCHGrid then
      begin
        With TCHGrid(Controls[i]) Do begin
          Options := Options - [goEditing, goColSizing, goRowSizing, goRangeSelect];
        end;
      end;
      }
    end;
    Top := 0;
    Left := 0;
    Show;
  end;
end;

procedure TFmCHDesign.SaveToStream(Stream: TStream);
begin
{  With CHChild Do begin
    SaveToStream(Stream, True);
  end;
  }
end;

procedure TFmCHDesign.MenuSaveClick(Sender: TObject);
begin
  ModalResult := mrOk;
end;

procedure TFmCHDesign.MenuOpenClick(Sender: TObject);
var
  F: TFileStream;
begin
  SetNoPause;
  if Not Assigned(FOpenDialog) then
  begin
    FOpenDialog := TOpenDialog.Create(Self);
  end;
  With FOpenDialog Do begin
    Filter := 'CHxml files (*.xml)|*.xml|All files (*.*)|*.*';
    if Execute then
    begin
      F := TFileStream.Create(FileName, fmOpenReadWrite);
      Try
        LoadFromStream(F);
      Finally
        F.Free;
      End;
    end;
  end;
end;

procedure TFmCHDesign.MenuSaveAsClick(Sender: TObject);
var
  S: String; 
  F: TFileStream;
begin
  SetNoPause;
  if Not Assigned(FSaveDialog) then
  begin
    FSaveDialog := TSaveDialog.Create(Self);
  end;
  With FSaveDialog Do begin
    Filter := 'CHxml files (*.xml)|*.xml|All files (*.*)|*.*';
    if Execute then
    begin
      if Length(ExtractFileExt(FileName)) = 0 then
        S := FileName + '.xml'
      else
        S := FileName;
      F := TFileStream.Create(S, fmCreate);
      Try
        SaveToStream(F);
      Finally
        F.Free;
      End;
    end;
  end;
end;

procedure TFmCHDesign.MenuExitClick(Sender: TObject);
begin
  Close;
end;

procedure TFmCHDesign.FormShow(Sender: TObject);
begin
  ObjType := 0;
  CHDesigner.Active := True;
  Align := alClient;
  Align := alNone;
end;

procedure TFmCHDesign.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SetNoPause;
  CHDesigner.Active := False;
end;

procedure TFmCHDesign.FormDestroy(Sender: TObject);
begin
  if Assigned(FSaveDialog) then
  begin
    FSaveDialog.Free;
    FSaveDialog := Nil;
  end;
  if Assigned(FOpenDialog) then
  begin
    FOpenDialog.Free;
    FOpenDialog := Nil;
  end;
  FCHChild.Free;
  FCHChild := Nil;
end;

procedure TFmCHDesign.FormCreate(Sender: TObject);
begin
  FCHChild := TFormLDSChild.Create(Self);
  With FCHChild Do begin
    ManualDock(ScrollBoxCHChild, Nil, alNone);
  end;
end;

procedure TFmCHDesign.SetNoPause;
begin
  With CHDesigner Do begin
    if Pause then
      ControlDesigning;
  end;
end;

procedure TFmCHDesign.ActionCHInspectorExecute(Sender: TObject);
begin
  With CHDesigner Do begin
    if Not Pause then
      ControlProperty;
  end;
end;

procedure TFmCHDesign.ActionCHDesignExecute(Sender: TObject);
begin
  With CHDesigner Do begin
    ControlDesigning;
  end;
end;

procedure TFmCHDesign.CHDesignerGetComponentsControl(Sender: TObject; var AControl: TWinControl);
begin
  AControl := FCHChild;
end;

procedure TFmCHDesign.CHDesignerCreateCHControl(Sender: TObject; X, Y: Integer; var ASuccess: Boolean);
var
  pt: TPoint;
  oControl: TWinControl;
begin
  ASuccess := (ObjType > 0);
  if ASuccess then
  begin
    oControl := TWinControl(Sender);
    pt := oControl.ScreenToClient(Point(X, Y));
    CreateCHControl(oControl, pt.X, pt.Y);
  end;
end;

procedure TFmCHDesign.CHDesignerDesignCHControl(Sender: TObject; ADesign: Boolean);
begin
  ToolButtonDesign.Down := ADesign;
end;

procedure TFmCHDesign.CHDesignerSelectCHControl(Sender: TObject; ASelection: TCHControlList);
begin
{  ActionCHDesign.Enabled := (ASelection.Count = 1) and
                            ( (ASelection[0] is TCHGrid) or
                              (ASelection[0] is TCHEleEdit) or
                              (ASelection[0] is TCHImage) );
}
end;

function TFmCHDesign.GetOpenRepDialog: TOpenDialog;
begin
  if Not Assigned(FOpenRepDialog) then
  begin
    FOpenRepDialog := TOpenDialog.Create(Self);
    FOpenRepDialog.InitialDir := GetAnyiHisSysPath;
    FOpenRepDialog.Filter := 'frf file(*.frf)|*.frf|all file(*.*)|*.*';
  end;
  Result := FOpenRepDialog;
end;

procedure TFmCHDesign.OpenRepFile;
begin
  With OpenRepDialog Do begin
    if Execute then
    begin
      RepFileName := FileName;
    end
    else begin
      RepFileName := '';
    end;
  end;
end;

procedure TFmCHDesign.DoPreviewBeforePrint(Sender: TObject; var ASuccess: Boolean);
begin
  ASuccess := True;
end;

procedure TFmCHDesign.DoPreviewAfterPrint(Sender: TObject; var ASuccess: Boolean);
begin
  ASuccess := True;
end;

procedure TFmCHDesign.DoGetPrintRep(Sender: TObject; var ARepData: OleVariant; APNBM: String; APreview: Boolean);
var
  sFileName: String;
  M: TMemoryStream;
begin
  if RepFileName = '' then
  begin
    OpenRepFile;
  end;
  sFileName := RepFileName; // ChangeFileExt(RepFileName, '.frf');
  if FileExists(sFileName) then
  begin
    M := TMemoryStream.Create;
    Try
      M.LoadFromFile(sFileName);
      StreamToByteArray(TVarData(ARepData), M);
      APNBM := '0';
    Finally
      M.Free;
    End;
  end
  else begin
    MessageDlg(Format('Print file rep not found! "%S"', [sFileName]), mtError, [mbOK], 0);
  end;
end;

procedure TFmCHDesign.DoGetPreviewRepList(Sender: TObject; var AIStrings: IAYStrings; var ADefaultIndex: Integer);
begin
  if RepFileName = '' then
  begin
    OpenRepFile;
  end;
  AIStrings.Clear;
  AIStrings.Add(Format('0=%S', [RepFileName]));
end;

procedure TFmCHDesign.MenuPrintClick(Sender: TObject);
begin
{  With CHChild Do
  begin
    OnGetPrintRep := DoGetPrintRep;
    OnGetPreviewRepList  := DoGetPreviewRepList;
    OnPreviewBeforePrint := DoPreviewBeforePrint;
    OnPreviewAfterPrint  := DoPreviewAfterPrint;
    Try
      RepFileName := '';
      Print;
    Finally
      OnGetPrintRep := Nil;
      OnGetPreviewRepList  := Nil;
      OnPreviewBeforePrint := Nil;
      OnPreviewAfterPrint  := Nil;
    End;
  end;
  }
end;

procedure TFmCHDesign.MenuPreviewClick(Sender: TObject);
begin
{  With CHChild Do begin
    OnGetPrintRep := DoGetPrintRep;
    OnGetPreviewRepList  := DoGetPreviewRepList;
    OnPreviewBeforePrint := DoPreviewBeforePrint;
    OnPreviewAfterPrint  := DoPreviewAfterPrint;
    Try
      RepFileName := '';
      Preview;
    Finally
      OnGetPrintRep := Nil;
      OnGetPreviewRepList  := Nil;
      OnPreviewBeforePrint := Nil;
      OnPreviewAfterPrint  := Nil;
    End;
  end;
  }
end;

initialization
begin
  RegisterClasses([TLDSLabel,TLDSEdit,TLDSPanel,TLDSCheckBox]);
end;

end.
